package ru.ifmo.mlarinsky.classificator.map;

import fi.javasom.jsom.SomNode;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import ru.ifmo.mlarinsky.classificator.model.Instrument;
import ru.ifmo.mlarinsky.classificator.model.InstrumentRegister;
import ru.ifmo.mlarinsky.classificator.model.QuoteDate;

public class MapNode extends SomNode {
    public static final int DETERMINATION = 1;
    public final int x;
    public final int y;
    private final List<Instrument> instruments;
    private final String[] dimensions;
    private Color color;
    private Object value;

    public MapNode(MapNode node) {
        this.x = node.x;
        this.y = node.y;
        this.instruments = node.instruments;
        this.dimensions = node.dimensions;
    }

    public MapNode(SomNode node, String[] dimensions, int x, int y) {
        super(node.getValues(), node.getLocation());
        setLabel(node.getLabel());
        
        this.x = x;
        this.y = y;
        this.instruments = parseLabelString(node.getLabel());
        this.dimensions = dimensions;
    }

    public List<Instrument> getInstruments() {
        return instruments;
    }

    public String[] getDimensions() {
        return dimensions;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public double getValue(int statistic, Object... parameters) {
        if (instruments == null || instruments.isEmpty()) {
            return 0;
        }

        switch (statistic) {
            case DETERMINATION:
                validateParameters(statistic, parameters);
                return 0;
            default:
                throw new IllegalArgumentException("Unknown statistic");
        }
    }

    private List<Instrument> parseLabelString(String labelsString) {
        if (labelsString == null || labelsString.length() == 0) {
            return Collections.emptyList();
        }

        List<Instrument> instrumentList = new ArrayList<Instrument>();

        String[] labels = labelsString.replaceAll(" ", "").split(",");
        for (String label : labels) {
            instrumentList.add(InstrumentRegister.getInstrument(label));
        }

        return instrumentList;
    }

    private void validateParameters(int statistic, Object... parameters) {
        switch (statistic) {
            case DETERMINATION:
                if (parameters == null || parameters.length != 2
                        || !(parameters[0] instanceof QuoteDate) || !(parameters[1] instanceof Integer)) {
                    StringBuilder stringBuilder = new StringBuilder("Parameters are: ");
                    if (parameters != null) {
                        for (Object object : parameters) {
                            stringBuilder.append(object).append(", ");
                        }
                    } else {
                        stringBuilder.append("null");
                    }

                    throw new IllegalArgumentException("Parameters should be: [QuoteDate as period start; "
                            + "Integer as period length]. " + stringBuilder.toString());
                }
            default:
                throw new IllegalArgumentException("Unknown statistic");
        }
    }
}
